package com.cqut.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;

import com.cqut.dto.SignUpManagerDTO;
import com.cqut.dto.TeamMemberDTO;
import com.cqut.dto.UserDTO;
import com.cqut.model.SignUpManager;
import com.cqut.dto.LimitShowDTO;
import com.cqut.service.ISignUpManagerService;
import com.cqut.util.ResponseUtil;

@RestController
@RequestMapping("/api/signUpManager")
public class SignUpManagerController {
    @Resource(name="signUpManagerService")
	private ISignUpManagerService signUpManagerService;

	/**
	 * 初始化列表
     * @param limitShowDTO
	 * @param errors
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<String> list(
	    String competeName, String stateName, String state,
	    @Validated
	    LimitShowDTO limitShowDTO, BindingResult errors) {
	     if(errors.hasErrors())
	         return ResponseUtil.getResponseEntityWhenInvalidReqParams();
	      
	     Map<String, Object> result = new HashMap<String, Object>();
	     List<Map<String, Object>> signUpList = signUpManagerService.findSignUpListWithLimit(competeName, stateName, state, limitShowDTO);
	     long count = signUpManagerService.findCountOfSignUpList(competeName, stateName, state);
	       
	     result.put("rows", signUpList);
	     result.put("total", count);
	     return ResponseUtil.getResEntityForGetAndJson(result); 
	}
	    
	 /**
	  * 审核通过
	  * */
    @RequestMapping(value = "/pass", method = RequestMethod.POST)
	public ResponseEntity<String> passSignUpMens(
	    		@RequestParam("passIds")
	    		String[] passIds) {
	    	
    	if(passIds == null || passIds.length == 0) {
	        return ResponseUtil.getResponseEntityWhenInvalidReqParams();	 
	    }
	    	 
	    if(signUpManagerService.passSignUpMenbers(passIds)) {
	    	return ResponseUtil.getResEntityForGetWhenSuccess();
	    }
	    else {
	    	return ResponseUtil.getResEntityForGetWhenFail();
	    } 	
    }
	    
	 /**
	  * 审核退回
	  * */
    @RequestMapping(value = "/back", method = RequestMethod.POST)
	public ResponseEntity<String> backSignUpMens(
	    @RequestParam("backIds")
	    String[] backIds){
	    	
	    if(backIds == null || backIds.length == 0) {
	   	    return ResponseUtil.getResponseEntityWhenInvalidReqParams();	 
	   	}
	    	
	    if(signUpManagerService.backSignUpMenbers(backIds)) {
			return ResponseUtil.getResEntityForPPPWhenSuccess();
	    }
		else {
			return ResponseUtil.getResEntityForPPPWhenFail();
		}    	
	}
	    
	/**
	 * 得到用户信息
	 * */
	 @RequestMapping(value = "/detail", method = RequestMethod.GET)
	 public ResponseEntity<String> getMessage(    		
	    	String signUpId) {
	     if(signUpId == null)
	         return ResponseUtil.getResponseEntityWhenInvalidReqParams();

		 Map<String, Object> menber = signUpManagerService.signUpIdDetail(signUpId);
		 return ResponseUtil.getResEntityForGetAndJson(menber);
	}
	    
	/**
	 * 得到老师信息
	 * */
	@RequestMapping(value = "/teacherDetail", method = RequestMethod.GET)
	public ResponseEntity<String> getTeacherMessage(    		
	    String signUpId) {
	    if(signUpId == null)
		    return ResponseUtil.getResponseEntityWhenInvalidReqParams();
	        
	    Map<String, Object> result = new HashMap<String, Object>();
	    List<Map<String, Object>> menber = signUpManagerService.signUpIdTeacher(signUpId);
	        
	    result.put("rows", menber);	                 
	    return ResponseUtil.getResEntityForGetAndJson(result);   		  		 		  
	}
	    
	/**
	 * 得到负责人信息
	 * */
	@RequestMapping(value = "/mainDetail", method = RequestMethod.GET)
	public ResponseEntity<String> getMainMessage(    		
	    String signUpId) {
	    if(signUpId == null)
	    	return ResponseUtil.getResponseEntityWhenInvalidReqParams();

	    Map<String, Object> result = new HashMap<String, Object>();
	    List<Map<String, Object>> menber = signUpManagerService.signUpIdMain(signUpId);
	        
	    result.put("rows", menber);	      	       
	    return ResponseUtil.getResEntityForGetAndJson(result);
	}
	    
	/**
	 * 得到成员信息
	 * */
	@RequestMapping(value = "/menberDetail", method = RequestMethod.GET)
	public ResponseEntity<String> getMenberMessage(    		
	    String signUpId) {
	    if(signUpId == null)
	        return ResponseUtil.getResponseEntityWhenInvalidReqParams();

	    Map<String, Object> result = new HashMap<String, Object>();	       
	    List<Map<String, Object>> menber = signUpManagerService.signUpIdMenber(signUpId);
	        
	    result.put("rows", menber);	           
	    return ResponseUtil.getResEntityForGetAndJson(result);   	       
	 }
	    
	 /**
	  * 得到阶段名称
      * */
	@RequestMapping(value = "/state", method=RequestMethod.GET)
	public ResponseEntity<String> getStateName() {	
		Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String, Object>> states = signUpManagerService.getStateName();
			
		result.put("rows", states);
		return ResponseUtil.getResEntityForGetAndJson(result);
	}
		
	/**
     * 得到状态
     * */
	@RequestMapping(value = "/stage", method=RequestMethod.GET)
	public ResponseEntity<String> getState() {	
		Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String, Object>> states = signUpManagerService.getState();
		     
		result.put("rows", states);
		return ResponseUtil.getResEntityForGetAndJson(result);
	}
	
	/**
	 * 新增组长
	 */
	@RequestMapping(value = "addGroupMember", method = RequestMethod.POST)
    public ResponseEntity<String> addUser(
        @Validated
        TeamMemberDTO teamMemberDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        //User userModel = userDTO.toModel();

        return ResponseUtil.getResEntityForPPP(signUpManagerService.menberAdd(teamMemberDTO));
    }
	
	/**
	 * 根据报名ID向里面添加成员
	 */
	@RequestMapping(value = "addGrouper", method = RequestMethod.POST)
    public ResponseEntity<String> addGroupers(
        @Validated
        TeamMemberDTO teamMemberDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        //User userModel = userDTO.toModel();

        return ResponseUtil.getResEntityForPPP(signUpManagerService.grouperAdd(teamMemberDTO));
    }
	
	
	
	
	/**
	 * 新增报名表
	 */
	@RequestMapping(value = "addApply", method = RequestMethod.POST)
    public ResponseEntity<String> addApplyInfo(
        @Validated
        SignUpManagerDTO signUpManagerDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        //User userModel = userDTO.toModel();

        return ResponseUtil.getResEntityForPPP(signUpManagerService.applyAdd(signUpManagerDTO));
    }
	
	/**
     * 成员列表
     * @param teamMember
     * @param limitShowDTO
     * @param errors
     * @return
     */
    @RequestMapping(value = "/memberList", method = RequestMethod.GET)
    public ResponseEntity<String> passList(
    	/*@RequestParam("teamMember")
        String teamMember,*/
    	TeamMemberDTO teamMemberDTO, BindingResult uErrors,
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();
        
        Map<String, Object> result = new HashMap<String, Object>();

        List<Map<String, Object>> memberList = signUpManagerService.getMemberList(teamMemberDTO.getSignUpId(),limitShowDTO);
        long count = signUpManagerService.getMemberListCount(teamMemberDTO.getSignUpId());

        result.put("rows", memberList);
        result.put("total", count);
        return ResponseUtil.getResEntityForGetAndJson(result);
    }
}
